package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.entity.EcProduct;
import com.bytz.modules.cms.product.model.EcProductModel;
import com.bytz.modules.cms.product.service.IEcProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "ec受控产品")
@RequestMapping("/ecProduct")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.EcProduct.EDIT + "')")
public class EcProductController extends BytzController<EcProduct, IEcProductService> {

    @Autowired
    private IEcProductService ecProductService;

    @ApiOperation(value = "保存或编辑", notes = "保存或编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<EcProduct> saveOrUpdate(@RequestBody EcProduct ecProduct) {
        ecProductService.saveEcProduct(ecProduct);
        return Result.ok(ecProduct);
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String productNumber) {

        return Result.ok(ecProductService.removeEcById(productNumber));
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<EcProductModel>> page(EcProductModel ecProduct,
                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              HttpServletRequest req) throws Exception {

        return Result.ok(ecProductService.queryPage(new Page<>(pageNo, pageSize), ecProduct, req.getParameterMap()));
    }

    @ApiOperation(value = "导入ec产品", notes = "导入ec产品")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {

        List<EcProduct> ecProductList = ExcelUtil.importGroup(request, EcProduct.class, null);
        return Result.ok(ecProductService.mergeDb(ecProductList));
    }

    @ApiOperation(value = "导出excel", notes = "导出excel")
    @RequestMapping(value = "/exportXls", method = RequestMethod.POST)
    public void export(HttpServletResponse response) throws Exception {

        ecProductService.export(response);
    }


    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("导入ec受控产品", "Sheet1", EcProduct.class, response);
    }

}
